package com.mogist.hqc.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.mogist.hqc.R;

import talex.zsw.basecore.util.RegTool;
import talex.zsw.basecore.util.glide.GlideTool;

public class PlayImageView extends RelativeLayout
{
	private ImageView vImageView;
	private ImageView vIvPlay;
	private RelativeLayout layout;
	private String imgUrl;
	private String videoUrl;
	private PlayListener listener;

	public PlayImageView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		initView(context);
	}

	public PlayImageView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView(context);
	}

	public PlayImageView(Context context)
	{
		super(context);
		initView(context);
	}

	private void initView(final Context context)
	{
		View rootView = LayoutInflater.from(context).inflate(R.layout.view_play_image, this);

		layout = (RelativeLayout) rootView.findViewById(R.id.layout);
		vImageView = (ImageView) rootView.findViewById(R.id.vImageView);
		vIvPlay = (ImageView) rootView.findViewById(R.id.vIvPlay);
	}

	public void setData(String imgUrl, String videoUrl, PlayListener listener)
	{
		this.imgUrl = imgUrl;
		this.videoUrl = videoUrl;
		this.listener = listener;
		if(RegTool.isEmpty(videoUrl))
		{
			vIvPlay.setVisibility(View.GONE);
			GlideTool.loadImg(vImageView,imgUrl);
		}
		else
		{
			vIvPlay.setVisibility(View.VISIBLE);
			Glide.with(getContext())
			     .setDefaultRequestOptions(new RequestOptions()
				                               .frame(0)
				                               .centerCrop()
				                               .error(new ColorDrawable(Color.BLACK))
				                               .placeholder(new ColorDrawable(Color.BLACK)))
			     .load(videoUrl)
			     .into(vImageView);
		}

		vIvPlay.setOnClickListener(new OnClickListener()
		{
			@Override public void onClick(View v)
			{
				if(listener != null)
				{
					listener.onPlay(imgUrl, videoUrl);
				}
			}
		});
	}

	public interface PlayListener
	{
		void onPlay(String imgUrl, String videoUrl);
	}

	public String getImgUrl()
	{
		return imgUrl;
	}

	public void setImgUrl(String imgUrl)
	{
		this.imgUrl = imgUrl;
	}

	public String getVideoUrl()
	{
		return videoUrl;
	}

	public void setVideoUrl(String videoUrl)
	{
		this.videoUrl = videoUrl;
	}

	public PlayListener getListener()
	{
		return listener;
	}

	public void setListener(PlayListener listener)
	{
		this.listener = listener;
	}

	public ImageView getImageView()
	{
		return vImageView;
	}

	public ImageView getIvPlay()
	{
		return vIvPlay;
	}

	public RelativeLayout getLayout()
	{
		return layout;
	}
}
